<template>
  <div class="waterMeterLeftTree">
    <div class="title">
      <span>选择设备</span>
      <button class="titleButton" @click="clickLeft">
        <i class="el-icon-caret-left"></i>
      </button>
    </div>
    <div class="search">
      <el-input placeholder="请输入监测点名称" v-model="filterText" class="searchFont" clearable @change="filterChange" suffix-icon="el-icon-search">
      </el-input>
      <!-- <i class="el-icon-d-caret"></i> -->
      <!-- <button class="searchButton" @click="changeDefault">
            <i class="el-icon-d-caret"></i>
        </button> -->
      <!-- <i class="el-icon-caret-top"></i> -->
    </div>
    <!-- {{areaData}} -->
    <div class="tree-container filter-tree" style="overflow: auto; margin-right: 10px;">
      <el-tree id="fixtree" class="tree el-scrollbar" ref="elTree" :data="treeData" :props="defaultProps" :default-expand-all="defaultTree" style="font-size: 12px" node-key="id" :filter-node-method="filterNode" @node-click="handleNodeClick" @node-expand="handleNodeExpand" show-checkbox :style="{ height: scrollerHeight, overflow: 'auto', minWidth: '100%' }" @check-change="selectTreeNode">
        <span class="custom-tree-node" slot-scope="{ node, data }">
          <span> <i :class="data.icon"></i>&nbsp;{{ node.label }} </span>
        </span>
        <!-- icon-class='el-icon-folder' -->
      </el-tree>
    </div>
  </div>
</template>

<script>
import {
  getTree, //区域树--小区资料全部
} from "@/api/baseManage/flScheduleCommunity/index";
import {
  searchflPipeMonitorPoint, //查询区域组织的监测点
  searchFlMonitorPoint,// 查询监测点详情
} from "@/api/schedule/base/flOverviewPipe/index";
export default {
  props: [
    "treeId"
  ],
  data () {
    return {
      treeData: [],
      areaData: [],
      expandDefault: [], //默认选中第一项
      //   展开开关
      defaultTree: true,
      filterText: "",
      defaultProps: {
        children: "children",
        label: "label",
      },
      clickNode: "", // 存放点击节点
      getTreeList: [], //存放父树
      childNodeData: {}, // 存放子节点数据
      oRhydrantData: [],
      hydrantIds: "", //消防栓ids
      treeCheckId: null
    };
  },

  async created () {
    await this.getTreeData();
  },
  computed: {
    scrollerHeight: function () {
      return window.innerHeight - 250 + "px";
    },
  },
  watch: {
    clickNode: {
      handler: function (val, olVal) {
        this.getsonNode(val);
      },
      deep: true,
    },
    oRhydrantData: {
      handler (val) {
        //递归替换
        console.log(val)
        debugger;
        this.changeTreeData(this.areaData, val)
        this.changeLabel(this.treeData)
        this.$nextTick(() => {
          let id
          id = this.treeData[0].children[0].children[0].id
          this.$refs.elTree.setCheckedKeys([id])
          // this.getHydrantInfo(id)
        })
      }
    },
    // treeId: {
    //   handler (val) {
    //     console.log(val)
    //     if (val) {
    //       this.treeCheckId = val
    //       if(this.treeData) {
    //         this.$refs.elTree.setCheckedKeys([val])
    //         this.$emit('handleId', id)
    //         this.getHydrantInfo(id)
    //       }
    //     }
    //   }
    // }
  },
  methods: {
    //当前树节点被选中
    selectTreeNode (data, checked, indeterminate) {
      if (checked) {
        const arr = [data.id]
        this.$refs.elTree.setCheckedKeys(arr)
        if(!data.children){
          this.$emit('getSearch', data.id)
        } 
      }
    },
    changeTreeData (areaData, val) {
      let comparData = val
      this.treeData = areaData.map(item => {
        if (item.children) {
          this.changeTreeData(item.children, comparData)
        } else {
          comparData.map(i => {
            if (item.code == i.code) {
              item = i
            }
          })
        }
        return item
      })
      console.log(this.treeData)
    },
    filterNode: function (value, data) {
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },
    filterChange: function () {
      // 这里每当输入的数据有变化就触发原生的过滤节点这个函数
      this.$refs.elTree.filter(this.filterText);
    },

    handleNodeClick (nodeData, node, nodeComponent) {
      this.clickNode = nodeData.sysCompanyCode;
      if (nodeData.monitorPointNo) {
        this.childNodeData = nodeData;
        this.$emit("getSearch", this.childNodeData);
      }
    },
    // 展开
    handleNodeExpand () { },

    // 点击按钮
    clickLeft () {
      this.$emit("clickClose", "点击隐藏");
    },

    // 获取树资料
    async getTreeData (newVal) {
      let { data } = await getTree();
      let dataList = []
      this.getTreeList = data;
      this.getTreeList.map(item => {
         if (item.name == "泵房") {
            dataList.push(item)
         }
      })
      this.areaData = dataList;
      this.changeLabel(this.areaData);
      this.getsonNode(this.areaData)
      console.log(this.areaData)
    },
    // 获取站点数据
    getsonNode (val) {
      console.log(val)
      debugger
      val.map(item => {
        if (item.children) {
          this.getsonNode(item.children)
        }
        else {
          let obj = {
            organization: item.code
          }
          searchflPipeMonitorPoint(obj).then(reponse => {
            item["children"] = reponse.data
            this.oRhydrantData.push(item)
          }).catch(error => {
          })
        }
      })
    },
    // 转为label
    changeLabel (arr) {
      arr.forEach((item) => {
        for (var key in item) {
          if (key === "name") {
            item["label"] = item[key];
            // delete item[key]
          }
          if (key === "children" && item[key].length) {
            var arr1 = item[key];
            this.changeLabel(arr1);
          } else {
          }
        }
      });
    },
  },
  computed: {
    scrollerHeight: function () {
      return window.innerHeight - 250 + "px";
    },
  },
};
</script>

<style lang="scss" scoped>
.waterMeterLeftTree {
  /deep/.el-tree-node__content {
    // padding: 0 !important;
    height: 32px;
  }
  border-right: 1px solid #e6e6e6;
  border-bottom: 1px solid #e6e6e6;
  padding: 2px;
  // height: 85vh;
  // height: 100vh;
  max-width: 100%;
  // max-height: 200px;
  overflow:scroll .filter-tree {
    font-size: 12px !important;
    margin-right: 10px !important;
  }
  /deep/ .el-tree .el-tree-node .is-leaf + .el-checkbox .el-checkbox__inner {
    display: inline-block;
  }
  /deep/ .el-tree .el-tree-node .el-checkbox .el-checkbox__inner {
    display: none;
  }
  #fixtree {
    display: inline-block;
  }

  .tree /deep/ .el-tree-node {
    position: relative;
    padding-left: 10px;
  }

  .tree /deep/ .el-tree-node__children {
    // padding-left: 12px;
  }

  .tree /deep/ .el-tree-node :last-child:before {
    height: 38px;
  }

  .tree /deep/ .el-tree > .el-tree-node:before {
    border-left: none;
  }

  .tree-container /deep/ .el-tree > .el-tree-node:after {
    border-top: none;
  }

  .tree /deep/ .el-tree-node:before {
    content: "";
    left: -4px;
    position: absolute;
    right: auto;
    border-width: 1px;
  }

  .tree /deep/ .el-tree-node:after {
    content: "";
    left: -4px;
    position: absolute;
    right: auto;
    border-width: 1px;
  }

  // .tree /deep/ .el-tree-node:before {
  //   border-left: 1px dashed #b8b9bb;
  //   bottom: 0px;
  //   height: 100%;
  //   top: -26px;
  //   width: 1px;
  // }

  // .tree /deep/ .el-tree-node:after {
  //   border-top: 1px dashed #b8b9bb;
  //   height: 20px;
  //   top: 12px;
  //   width: 24px;
  // }

  // ---------------------------------
  //有子节点 且未展开
  // .el-tree /deep/ .el-icon-caret-right:before {
  //   background: url("./image/xinzeng1.png") no-repeat 0 0;
  //   content: "";
  //   display: block;
  //   width: 10px;
  //   height: 10px;
  //   font-size: 10px;
  //   background-size: 10px;
  // }
  //有子节点 且已展开
  // .el-tree
  //   /deep/
  //   .el-tree-node__expand-icon.expanded.el-icon-caret-right:before {
  //   background: url("./image/jian.png") no-repeat 0 0;
  //   content: "";
  //   display: block;
  //   width: 10px;
  //   height: 10px;
  //   font-size: 10px;
  //   background-size: 10px;
  // }
  // 没有子节点
  // .el-tree /deep/.el-tree-node__expand-icon.is-leaf::before {
  //   background: url("./image/jian.png") no-repeat 0 0;
  //   content: "";
  //   display: block;
  //   width: 10px;
  //   height: 10px;
  //   font-size: 10px;
  //   background-size: 10px;
  // }
  .title {
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid #e6e6e6;
    // font-weight: 700;
    padding: 10px 0 10px 10px;
    // .el-icon-caret-left{
    //     float:right;
    //     margin-right: -10px;
    // }
    .titleButton {
      width: 20px;
      height: 20px;
      line-height: 20px;
      border: 0;
      padding: 0;
      margin-right: -2px;
      .el-icon-caret-left {
        margin: -10px 0px 0px 0px;
        font-size: 16px;
      }
    }
  }
  .search {
    display: flex;
    justify-content: center;
    align-items: center;
    .searchFont {
      margin: 10px 5px;
      padding: 5px 2px;
      /deep/.el-input__inner {
        // width: 166px;
        height: 30px;
        line-height: 30px;
        padding: 5px 2px;
        width: 100%;
        // line-height: 30px;
        // padding: 5px 5px;
      }
      // .el-input__suffix {
      //     .el-input__suffix-inner {
      //     border-color: none;
      //     .el-icon-circle-close:before {
      //         content: "\e79d" !important;
      //         font-size: 18px;
      //     }
      //     }
      // }
    }
    .searchButton {
      border: 0;
      padding: 0;
      float: right;
      .el-icon-d-caret {
        margin: 0;
      }
    }
    .el-input__inner {
      margin-top: 15px;
    }
    .el-icon-search {
      position: relative;
      top: 10px;
      right: 5px;
      cursor: pointer;
    }
  }
}
</style>
